import axios from "axios";
import router from "@/router";

const BASE_URL = '/api';
const BASE_URL1 = '/dowloadapi';

axios.defaults.timeout = 100000; // 超时时间设置
axios.defaults.withCredentials = true; // true允许跨域
// axios.defaults.baseURL = BASE_URL;
// Content-Type 响应头
axios.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded;charset=UTF-8";

// 响应拦截器
axios.interceptors.response.use(
  (response) => {
    // 如果返回的状态码为200，说明接口请求成功，可以正常拿到数据
    // 否则的话抛出错误
    if (response.status === 200||response.status===206) {
      return Promise.resolve(response);
    } else {
      return Promise.reject(response);
    }
  },
  // 服务器状态码不是2开头的的情况
  (error) => {
    if (error.response.status) {
      switch (error.response.status) {
        // 401: 未登录
        case 401:
          router.replace({
            path: "/",
            query: {
              // redirect: router.currentRoute.fullPath
            },
          });
          break;
        case 403:
          // console.log('管理员权限已修改请重新登录')
          // 跳转登录页面，并将要浏览的页面fullPath传过去，登录成功后跳转需要访问的页面
          setTimeout(() => {
            router.replace({
              path: "/",
              query: {
                // redirect: router.currentRoute.fullPath
              },
            });
          }, 1000);
          break;

        // 404请求不存在
        case 404:
          // console.log('请求页面飞到火星去了')
          break;
      }
      return Promise.reject(error.response);
    }
  }
);

export function getBaseURL() {
  return BASE_URL;
}

/**
 * 封装get方法
 * @param url
 * @param data
 * @param bool默认false代理为/api true代理为BASE_URL1
 * @returns {Promise}
 */
export function get(url, params?: object,bool=false) {
  if(axios.defaults.headers['range']||axios.defaults['responseType']){
    delete  axios.defaults.headers['range']
    delete axios.defaults['responseType']
  }
  return new Promise((resolve, reject) => {
    axios.get(bool?`${BASE_URL1}${url}`:`${BASE_URL}/${url}`, params).then(
      (response) => resolve(response.data),
      (error) => reject(error)
    );
  });
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function post(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.post(`${BASE_URL}${url}`, data).then(
      (response) => resolve(response.data),
      (error) => reject(error)
    );
  });
}

/**
 * 封装delete请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function deletes(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.delete(`${BASE_URL}${url}`,data).then(
      (response) => resolve(response.data),
      (error) => reject(error)
    );
  });
}

/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function put(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.put(`${BASE_URL}${url}`, data).then(
      (response) => resolve(response.data),
      (error) => reject(error)
    );
  });
}

export function headers(url){
  if(axios.defaults.headers['range']||axios.defaults['responseType']){
    delete  axios.defaults.headers['range']
    delete axios.defaults['responseType']
  }

  return new Promise((resolve,rej)=>{
    axios.head(`${BASE_URL1}${url}`).then((result) => {
      return resolve(result.headers['content-length'])
    }).catch((err) => {
      rej(err)
    });
  })
}

export function download(url,start,end,i){
  console.log('axios.defaults',axios.defaults)
  axios.defaults.headers["range"]=`bytes=${start}-${end}`
  axios.defaults.responseType='blob'
  return new Promise((req,rej)=>{
    axios.get( `${BASE_URL1}${url}`).then((result) => {
      result.data.arrayBuffer().then((result) => {
        req({i,buffer:result})
      
      
      }).catch((err) => {
        console.log(err)
      });
    }).catch((err) => {
      rej(err)
    });

  })
}

